from concurrent.futures import ThreadPoolExecutor

from main.config import max_workers

executor = None


def get_executor():
    global executor
    if executor is None:
        executor = ThreadPoolExecutor(max_workers)
    return executor


class FutureManagement:
    def __init__(self):
        self.future_list = []

    # 定义类变量，用于保存单例实例
    _instance = None

    # 重写类的构造函数
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

    def add_a_future(self, name, future):
        self.future_list.append((name, future))

    def exist_future(self):
        return False if(len(self.future_list) == 0) else True

    def patrol(self):
        for name, future in self.future_list:
            if future.done():
                self.future_list.remove((name, future))
                yield name

    def size(self):
        return len(self.future_list)
